﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace JEngine.Framework
{
    /// <summary>
    /// UI容器
    /// </summary>
    [RequireComponent(typeof(Canvas))]
    [RequireComponent(typeof(GraphicRaycaster))]
    public class UIContainer : UIBase
    {
        /// <summary>
        /// 画布
        /// </summary>
        protected Canvas canvas;

        /// <summary>
        /// 图形投射器
        /// </summary>
        protected GraphicRaycaster graphicRaycaster;

        /// <summary>
        /// 子容器
        /// </summary>
        Dictionary<string, ChildPanel> childPanel = new Dictionary<string, ChildPanel>();

        /// <summary>
        /// 子canvas射线投射器
        /// </summary>
        protected List<GraphicRaycaster> graphicRaycasterList = new List<GraphicRaycaster>();

        /// <summary>
        /// 当前子面板
        /// </summary>
        ChildPanel curChildPanel;

        public object[] parameters;
      
        /// <summary>
        /// 初始化组件
        /// </summary>
        protected override void InitComponent()
        {
            base.InitComponent();
            this.canvas = this.gameObject.AddMissingComponent<Canvas>();
            this.graphicRaycaster = this.gameObject.AddMissingComponent<GraphicRaycaster>();
        }

       

        /// <summary>
        /// 被打开时自动调用
        /// </summary>
        virtual public void OnOpen()
        {
            if (this.canvas.IsShow()) return;
            this.canvas.ShowByCanvas();
            CloseChildPanel();
            if (curChildPanel != null)
            {
                curChildPanel.gameObject.SetActive(true);
                curChildPanel.OnOpen();
            }

            SetGraphicRayCasterState(true);
        }


        /// <summary>
        /// 被关闭时自动调用
        /// </summary>
        virtual public void OnClose()
        {
            if (!this.canvas.IsShow()) return;
            CloseChildPanel();
            this.canvas.HideByCanvas();
            SetGraphicRayCasterState(false);
        }

        /// <summary>
        /// 添加投射器
        /// </summary>
        /// <param name="caster"></param>
        public void AddGraphicRaycaster(GraphicRaycaster caster)
        {
            graphicRaycasterList.Add(caster);
        }


        /// <summary>
        /// 移除投射器
        /// </summary>
        /// <param name="caster"></param>
        public void RemoveGraphicRaycaster(GraphicRaycaster caster)
        {
            graphicRaycasterList.Remove(caster);
        }

        /// <summary>
        /// 设置射线投射器的状态
        /// </summary>
        /// <param name="state"></param>
        private void SetGraphicRayCasterState(bool state)
        {
            this.graphicRaycaster.enabled = state;
            for (int i = 0; i < graphicRaycasterList.Count; i++)
            {
                this.graphicRaycasterList[i].enabled = state;
            }
        }

        /// <summary>
        /// 打开子界面
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parent"></param>
        /// <returns></returns>
        protected T OpenChildPanel<T>(Transform parent) where T : ChildPanel
        {
            CloseChildPanel();
            string name = typeof(T).Name;
            if (!childPanel.TryGetValue(name, out curChildPanel))
            {
                curChildPanel = Engine.UI.CreateChildPanel<T>(parent);
                childPanel.Add(name, curChildPanel);
            }

            curChildPanel.gameObject.SetActive(true);
            curChildPanel.OnOpen();
            return curChildPanel as T;
        }


        /// <summary>
        /// 关闭子界面
        /// </summary>
        void CloseChildPanel()
        {
            List<ChildPanel> list = ListPool<ChildPanel>.Get();
            list.AddRange(childPanel.Values);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].gameObject.SetActive(false);
                list[i].OnClose();
            }

            ListPool<ChildPanel>.Release(list);
        }


        // protected override void OnDestroy()
        // {
        //     base.OnDestroy();
        //     // if (AppConst.IsApplicationQuit) return;
        //     // App.ResourceMgr.UnLoadAsset(this.name);
        // }
    }
}